package org.nlp2rdf.navigator.client;

import org.nlp2rdf.navigator.client.common.ConceptDTO;
import org.nlp2rdf.navigator.client.common.LearnedConceptDTO;
import org.nlp2rdf.navigator.client.common.RelatedConceptsDTO;
import org.nlp2rdf.navigator.client.common.StoredConceptDTO;
import org.nlp2rdf.navigator.client.common.RelatedConceptsDTO.StrategyDTO;
import org.nlp2rdf.navigator.client.functionality.ConceptWidget;
import org.nlp2rdf.navigator.client.functionality.TitleWidget;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class RelatedConceptsWidget extends Composite {

    private Application app;

    private RelatedConceptsDTO relatedConcepts;

    private VerticalPanel conceptsGoHere = new VerticalPanel();

    private TitleWidget titleWidget = new TitleWidget("Related concepts", conceptsGoHere);

    public RelatedConceptsWidget(Application application) {

        this.app = application;

        //starts open
        titleWidget.show();

        VerticalPanel mainPanel = new VerticalPanel();
        mainPanel.add(titleWidget);
        conceptsGoHere.setWidth("100%");
        mainPanel.add(conceptsGoHere);
        mainPanel.addStyleName("module-border");
        mainPanel.setWidth("95%");
        mainPanel.addStyleName("greyed");
        initWidget(mainPanel);
    }

    private void draw() {

        conceptsGoHere.clear();

        for (StrategyDTO strategy : relatedConcepts.getStrategies()) {
//			Window.alert(strategy.toDebugString());
            if (strategy.getConcepts().isEmpty()) {
                continue;
            }
            // Display a header for the strategy
            VerticalPanel strategyContainer = new VerticalPanel();
            strategyContainer.setWidth("100%");
            Label stratTitle = new Label(strategy.getTitle());
            stratTitle.addStyleName("module-subtitle");
            strategyContainer.add(stratTitle);
            VerticalPanel existingOntologyClasses = new VerticalPanel();
            String classes = "";

            conceptsGoHere.add(strategyContainer);
            for (final ConceptDTO learningResult : strategy.getConcepts()) {
                if (learningResult instanceof LearnedConceptDTO) {
                    ConceptWidget w = new ConceptWidget(learningResult, app);
                    w.displayAsRelatedConcept();
                    strategyContainer.add(w);
                } else {
                    classes += learningResult.getLabelNotNull() + ", ";
                }
            }
            if (!classes.isEmpty()) {
                //existingOntologyClasses.add(new Label("Existing classes from the ontology:"));
                existingOntologyClasses.add(new Label(classes.trim().substring(0, classes.length()-2)));
                strategyContainer.add(existingOntologyClasses);
            }

        }

    }

    public void update(RelatedConceptsDTO relatedConcepts) {
        this.relatedConcepts = relatedConcepts;
        draw();

    }

}
